(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.feeCode.createVerifyController', createVerifyController);
	createVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'feeCodeService' ];
	function createVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, feeCodeService) {
		var ctrl = this, feeCodeInfo = $scope.$stateParams.params, errorMessage = {};
//		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.feeCode = {};
		ctrl.init = function() {
			// init tier criteria
			feeCodeService.initTierCriteria(layoutConfig, $scope, ctrl, true);
			// init options
			feeCodeService.initOptionData(ctrl, $scope, true).then(function() {
				authService.createVerify(feeCodeInfo).then(function(response) {
					var initFeeCode = response.data.model;
					initFeeCode.action = 'createVerify';
					feeCodeService.initLayoutAndModel($scope, ctrl, initFeeCode, layoutConfig, true).then(function() {
						ctrl.feeCode.taskId = response.data.task.taskId;
					});
				});
			});
		};

		ctrl.createVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.feeCode.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createVerifyApprove(ctrl.feeCode).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.createVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.feeCode.comments = comments;
						authService.createVerifyReject(ctrl.feeCode).then(rejectSuccessful, rejectFailed);
					}
				}
			});			
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.feecode.list',
					'state' : 'cda.feeCode.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.feecode.list',
					'state' : 'cda.feeCode.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);
